package com.token;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.ResourceBundle;

public class CheckToken {
	
	public void getKey(){
		KeyPairGenerator key = null;
		try {
            key = KeyPairGenerator.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        key.initialize(1024);
        // 产生密钥对
        KeyPair keyPair = key.generateKeyPair();
        // 得到公钥
        PublicKey publicKey = keyPair.getPublic();
        // 得到私钥
        PrivateKey privateKey = keyPair.getPrivate();
        byte[] keyBytes=publicKey.getEncoded();
        byte[] keyBytes2=privateKey.getEncoded();
        //base64加密,方便以字符串存入数据库或文件
        String temp = new sun.misc.BASE64Encoder().encodeBuffer(keyBytes);
        System.out.println("公钥"+temp);
        String temp2 = new sun.misc.BASE64Encoder().encodeBuffer(keyBytes2);
        System.out.println("私钥"+temp2);
	}

		    //验证私钥公钥
		    public boolean checkKey() {
		        try {
		            //获取数据库中的用户公钥
		        	ResourceBundle resourceBundle = ResourceBundle.getBundle("keyPair");
		    		String publicKeyText = resourceBundle.getString("publicKey");
		    		String privateKeyText = resourceBundle.getString("privateKey");
		            System.out.println("publicKeyText=="+publicKeyText);
		            System.out.println("privateKeyText=="+privateKeyText);
		            // base64解密
		            byte[] sikey = new sun.misc.BASE64Decoder().decodeBuffer(privateKeyText);
		            byte[] gongkey = new sun.misc.BASE64Decoder().decodeBuffer(publicKeyText);
		            // 将byte[]转成密钥
		            KeyFactory keyFactory = KeyFactory.getInstance("RSA"); // 指定算法RSA,得到一个KeyFactory的实例
		            // 解码私钥
		            PKCS8EncodedKeySpec pKCS8EncodedKeySpec =new PKCS8EncodedKeySpec(sikey);
		            PrivateKey privateKey2 = keyFactory.generatePrivate(pKCS8EncodedKeySpec);
		            // 解码公钥
		            KeyFactory keyFactory2 = KeyFactory.getInstance("RSA"); // 指定算法RSA,得到一个KeyFactory的实例
		            X509EncodedKeySpec x509KeySpec2 = new X509EncodedKeySpec(gongkey);
		            PublicKey publicKey2 = keyFactory2.generatePublic(x509KeySpec2);
		         
		            Signature sig = Signature.getInstance("MD5WithRSA");
		            // 获取私钥
		            sig.initSign(privateKey2);
		            byte[] signatureByte = sig.sign();
		            sig.initVerify(publicKey2);
		            boolean verified = false;
		            verified = sig.verify(signatureByte);
		            if (verified) {
		                System.out.println("验证成功");
		                return true;
		            } else {
		                System.out.println("验证失败");
		                return false;
		            }
		        } catch (Exception e) {
//		          e.printStackTrace();
		            System.out.println("验证失败2");
		            return false;
		        }
		    }
		    
		    public static void main(String[] args) {
		    	CheckToken checkToken = new CheckToken();
		    	//checkToken.getKey();
		    	checkToken.checkKey();
			}
}
